<?php
//
//	file: includes/wpu/class_wpu.php
//	begin: 10/05/2007
//	$Author: christopher $
//	$Revision: 14 $
//	$Date: 2007-10-08 14:55:03 +0000 (Mon, 08 Oct 2007) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: Integration with Wordpress

if ( !defined('IN_PHPBB') )
{
	die('Hack attempt');
}

if ( !defined('WPU_CURRENT_VERSION') )
{
	define('WPU_CURRENT_VERSION', '0.9.5');
	define('PAGE_BLOG', -1385);
	define('WP_INT_TABLE', $table_prefix.'wpu_integration');
}

// do not go further during login or installation
if ( defined('IN_LOGIN') || defined('IN_INSTALL') )
{
	return;
}

// intall directory still present
if ( file_exists(phpbb_realpath($config->root . 'install_wpu')) )
{
	if ( $config->data['mod_wp_united'] != WPU_CURRENT_VERSION )
	{
		header('Location: ' . $config->url('install_wpu/install'));
		exit;
	}
	else
	{
		message_die(GENERAL_MESSAGE, 'Please remove install_wpu/ directory');
	}
}

if ( !$config->data['mod_wp_united'] )
{
	return;
}

class wpu_version
{
	function read(&$data)
	{
		$data[] = array('app_name' => 'WP United', 'app_desc' => '', 'app_marker' => 'mod_wp_united', 'app_author' => 'impleri', 'app_author_url' => 'http://impleri.net/', 'app_server' => 'mods.impleri.net', 'app_file' => '/download/versions.dta', 'app_protocol' => 'native', 'app_page' => 'http://mods.impleri.net/download.php');
	}
}

if ( defined('IN_ADMIN') || in_array($requester, array('profile', 'usercp', 'viewtopic', 'privmsg')) )
{
	$wpu_lang_files = array('lang_extend_wpu');
	$user->use_lang_file($wpu_lang_files);
	$user->use_lang('wpcp');
	unset($wpu_lang_files);

	include($config->url('wp-united/mod-settings'));
	$wpSettings = ($wpSettings) ? $wpSettings : get_integration_settings();
	if ( ($wpSettings == FALSE)	|| ($wpSettings['wpPath'] == "") )
	{
		message_die(GENERAL_ERROR, $user->lang('WP_DBErr_Gen'), __LINE__, __FILE__, $sql);
	}

	function wpu_integrate($fields)
	{
		global $config, $view_user, $user, $wpSettings;

		if ( !empty($wpSettings['integrateLogin']) )
		{
			$wpID = $view_user->data['user_wpuint_id'];
			if (!empty($wpID))
			{
				$GLOBALS['wpu_newDetails'] = array(
					'username' => ($fields['username']) ? $fields['username'] : $view_user->data['username'],
					'user_email' => ($fields['user_email']) ? $fields['user_email'] : $view_user->data['user_email'],
					'user_password' => ($fields['user_password']) ? $fields['user_password'] : $view_user->data['user_password'],
					'user_website' => ($fields['user_website']) ? $fields['user_website'] : $view_user->data['user_website'],
					'user_aim' => ($fields['user_aim']) ? $fields['user_aim'] : $view_user->data['user_aim'],
					'user_yim' => ($fields['user_yim']) ? $fields['user_yim'] : $view_user->data['user_yim'],
					'user_avatar_type' => ($fields['user_avatar_type']) ? $fields['user_avatar_type'] : $view_user->data['user_avatar_type'],
					'user_allow_avatar' => ($fields['user_allowavatar']) ? $fields['user_allowavatar'] : (($fields['user_allow_avatar']) ? $fields['user_allow_avatar'] : $view_user->data['user_allowavatar']),
					'user_avatar' => ($fields['user_avatar']) ? $fields['user_avatar'] : $view_user->data['user_avatar'],
				);
				$GLOBALS['wpu_add_actions'] = '
					$wpUsrData = get_userdata($wpID);
					$wpUpdateData =	$wpUtdInt->check_details_consistency($wpUsrData, $GLOBALS[\'newDetails\']);
					if ( $wpUpdateData ) {
						wp_update_user($wpUpdateData);
					}
				';

				define('WPU_PERFORM_ACTIONS', TRUE);

				if ( $wpSettings['showHdrFtr'] != 'REV' )
				{
					//enter the integration
					require_once($phpbb_root_path . 'wp-united/wp-integration-class.' . $phpEx);
					require_once($phpbb_root_path . 'wp-united/abstractify.' . $phpEx);
					$wpUtdInt = WPU_Integration::getInstance(get_defined_vars());
					if ( !$wpUtdInt->can_connect_to_wp() )
					{
						message_die(GENERAL_ERROR, $lang['WP_Not_Installed_Yet'],'','','');
					}
					$wpUtdInt->enter_wp_integration();
					eval($wpUtdInt->exec());
					$wpUtdInt->exit_wp_integration();
					$wpUtdInt = null; unset($wpUtdInt);
				}
			}
		}
	}
}

// set our plugs
$config->set_plug_ins('mod_wp_united', array(
	'class_posts' => array(
		'layer' => 'includes/wpu/class_wpu_display',
		'object' => 'wpu_display',
	),
	'viewtopic' => array(
		'layer' => 'includes/wpu/class_wpu_display',
		'object' => 'wpu_display',
	),
	'class_versions' => array(
		'object' => 'wpu_version',
	),
));

?>